package com.yj.springbootsecuritydemo01.thread;

/**
 *
 */
public class DeadLockTest {

    private static Object resourceA = new Object();
    private static Object resourceB = new Object();


    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (resourceA){
                System.out.println(Thread.currentThread().getName() + "获取resourceA 资源");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "开始申请 resourceB 资源");
                synchronized (resourceB){
                    System.out.println(Thread.currentThread().getName() + "获取resourceB 资源");
                }
            }
        },"t1");

        Thread t2 = new Thread(() -> {
            synchronized (resourceB){
                System.out.println(Thread.currentThread().getName() + "获取resourceB 资源");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "开始申请 resourceA 资源");
                synchronized (resourceA){
                    System.out.println(Thread.currentThread().getName() + "获取resourceA 资源");
                }
            }
        },"t2");

        t1.start();
        t2.start();
    }
}
